package com.hotau.account;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.hotau.Executable;
import com.hotau.HotauActivity;
import com.hotau.HotauConstants;
import com.hotau.R;
import com.hotau.member.service.MemberService;
import com.hotau.util.DialogUtil;
import com.hotau.util.HttpRequestAsynTask;
import com.hotau.util.HttpUtil;

/**
 * 预付卡解绑
 * @author liujia (liujia9234@126.com)
 * @data @2011-12-17
 * @version 1.0
 * <ul>
 * 		<li>1.0 创建</li>
 * </ul>
 */
public class UnBindPaymentAdvanceFragment extends Fragment implements
		OnClickListener {
	
	
	private TextView unbind_tv_cardnum = null;
	private EditText unbind_et_paypwd = null;
	private Button unbind_btn_submit = null;
	private PaymentAdvanceManageFragment parentFrament = null;
	
	private String cardNo;

	@Override
	public void onActivityCreated(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onActivityCreated(arg0);
		initAllWidget();
		initAllEvent();
		unbind_tv_cardnum.setText(cardNo);
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.cardNo = getArguments().getString("cardNo");
		this.parentFrament = (PaymentAdvanceManageFragment)getArguments().get("parentFrament");
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup group, Bundle arg2) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.account_unbind_payment_advance, group, false);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.account_unbind_btn_submit:
			if("".equals(unbind_et_paypwd.getText().toString())){
				DialogUtil.showDialog(getActivity(), "", "支付密码不能为空");
				return;
			}
			final HotauActivity csmbpActivity = (HotauActivity) getActivity();
			HttpRequestAsynTask payment_advance_type_task = new HttpRequestAsynTask(csmbpActivity) {
				@Override
				protected void doPostExecute(String result) {
					try {
						JSONObject data = new JSONObject(result);
						if (data != null&& data.getInt("result") == 1) {
							// 解绑卡成功
							// DialogUtil.showDialog(csmbpActivity,
							// "", "解绑卡成功");
							DialogUtil.showDialog(csmbpActivity, "", "解绑成功",new Executable() {
								@Override
								public void execute() {
									// TODO Auto-generated method stub
									parentFrament.passValue(0, "");
									csmbpActivity.popFragment();
								}
							});
						} else {
							// 解绑卡卡失败
							DialogUtil.showDialog(csmbpActivity,"",data.getString("errorMsg"));
						}
					} catch (JSONException e) {
						// TODO Auto-generated
						// catch block
						DialogUtil.showDialog(csmbpActivity,"","解绑卡返回数据出错 ");
					}
				}
			};
			StringBuilder data = new StringBuilder();
			data.append("Form:token="+ MemberService.getToken(csmbpActivity));
			data.append(";cardNo="+ unbind_tv_cardnum.getText().toString());
			data.append(";payPwd=" + unbind_et_paypwd.getText().toString());
			data.append(";terminalCode=" + HotauConstants.getTerminalCode(getActivity()));
			payment_advance_type_task.execute(HotauConstants.ACCOUNT_UNBIND_CARD_INFO,data.toString(),HttpUtil.UTF8_ENCODING);
			break;
		default:
			break;
		}
	}
	
	/**
	 * 初始化所有控件
	 */
	public void initAllWidget() {
		unbind_tv_cardnum = (TextView) getActivity().findViewById(R.id.account_unbind_tv_cardnum);
		unbind_et_paypwd = (EditText) getActivity().findViewById(R.id.account_unbind_et_paypwd);
		unbind_btn_submit = (Button) getActivity().findViewById(R.id.account_unbind_btn_submit);
	}
	
	/**
	 * 初始化所有事件
	 */
	public void initAllEvent() {
		unbind_btn_submit.setOnClickListener(this);
	}

}
